<?php

namespace App\Http\Controllers\Admin;

use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;
use App\Api\Transfer;
use App\Repository\UpThree\GetThreeOrderRepostition;
use App\Api\CatchOrder\DouYin;
// 分销商
class AllOrderController extends BaseController
{
  public function __construct()
  {
  }
  public function index(Request $request)
  {
    $params = $request->all();
    // var_dump($params, 11111111111);
    $where = [];
    $whereDou = [];
    $whereLike = [];
    $whereHistory=[];
    // 订单号
    if (!empty($params['orderno'])) {
      $where[] = ['orders.ordersno', 'like', trim($params['orderno'], ' ') . '%'];
      // $where['orders.ordersno'] = trim($params['orderno'], ' ');
      $whereDou['dist_orderno'] = trim($params['orderno'], ' ');
      $whereHistory['order_num'] = trim($params['orderno'],' ');
    }
    //上游订单号
    if (!empty($params['sup_orderno'])) {
      $where['orders.sup_orderno'] = trim($params['sup_orderno'], ' ');
      $whereHistory['order'] = trim($params['orderno'],' ');
    }
    // 下游订单号
    if (!empty($params['dist_orderno'])) {
      $where['orders_user.dist_orderno'] = trim($params['dist_orderno'], ' ');
    }
    // 生产号码
    if (!empty($params['iccid'])) {
      $where['orders.iccid'] = trim($params['iccid'], ' ');
      $whereHistory['iccid'] = trim($params['iccid'],' ');
    }
    // 用户身份证
    if (!empty($params['pay_identity'])) {
      $where['orders_user.pay_identity'] = trim($params['pay_identity'], ' ');
      $whereDou['identity'] = trim($params['pay_identity'], ' ');
      $whereHistory['user_idcard'] = trim($params['pay_identity'],' ');
    }
    // 用户姓名
    if (!empty($params['contact_name'])) {
      $where['orders_user.contact_name'] = trim($params['contact_name'], ' ');
      $whereDou['name'] = trim($params['contact_name'], ' ');
      $whereHistory['user_name'] = trim($params['contact_name'],' ');
    }
    // 用户联系电话
    if (!empty($params['contact_phone'])) {
      $where['orders_user.contact_phone'] = trim($params['contact_phone'], ' ');
      $whereDou['contact'] = trim($params['contact_phone'], ' ');
      $whereHistory['user_phone'] = trim($params['contact_phone'],' ');
    }
    // 物流单号
    if (!empty($params['express_number'])) {
      $where['orders.express_number'] = trim($params['express_number'], ' ');
      $whereHistory['logistics_num'] = trim($params['express_number'],' ');
    }
    if (empty($where)) {
      return $this->returnMsg(200, '请输入查询条件', ['order' => [],'history'=>[], 'douyin' => [], 'old' => ['oldOrder' => [], 'oldDouyin' => []]]);
    }
    $return = [];
    $return['old'] = $this->getOldOder($where);
    // 查询本系统订单的数据  直接查询出来
    // var_dump($where);
    $shop_id = $request->user->shop_id;
    // DB::connection()->enableQueryLog();
    $return['order'] = DB::table('orders')->leftJoin('orders_user', 'orders_user.id', '=', 'orders.orders_user_id')->where($where)->where(function ($query) use ($shop_id) {
      $query->where(['orders.shop_id' => $shop_id])
        ->OrWhere([['orders.shop_str', 'like', '%.' . $shop_id . '.%']]);
    })->get()->toArray();
    // $log = DB::getQueryLog();
    // var_dump($log, 111111111);
    $flag = true;
    if (!empty($return['order'])) {
      $flag = false;
      $whereDou = [];
      foreach ($return['order'] as $key => &$v) {
        $whereDou[] = ['dist_orderno' => $v->dist_orderno];
        $v->pay_identity = '*********' . substr($v->pay_identity, 8, 3);
        $v->contact_phone = substr($v->contact_phone, 0, 3).'****' . substr($v->contact_phone, 6, 4);
      }
    }

    // 查询本地抖音表的数据  直接查询出来

    if (empty($whereDou)) {
      $return['douyin'] = [];
      return $this->returnMsg(200, '查询成功', $return);
    }
    if ($flag) {
      // DB::connection()->enableQueryLog();
      $return['douyin'] = DB::table('douyin_catch')->select(['goods_code', 'name', 'identity', 'contact', 'province', 'city', 'area', 'address', 'dist_orderno', 'dist_name', 'douyin_name', 'three_msg', 'status_desc', 'author_id', 'author_name', 'create_at', 'created_at'])->where($whereDou)->get();
      // $log = DB::getQueryLog();

      // dd($log);
    }
    if (!$flag) {
      // var_dump($whereDou);
      // DB::connection()->enableQueryLog();
      $return['douyin'] = DB::table('douyin_catch')->orWhere(function ($query) use ($whereDou) {
        // if (count($whereDou) > 1) {
        //   var_dump($whereDou);
        //   $query->orWhere($whereDou);
        // }
        foreach ($whereDou as $key => $v) {
          if ($key == 0) {
            // $query->where($v);
            // continue;
          }
          $query->orWhere($v);
        }
      })->get();
      
      // $log = DB::getQueryLog();
      // dd($log);
    }
    // 历史订单
    $resHistory = DB::table('order_history')->where($whereHistory)->get();
    // var_dump($resHistory,55555555555);
    // if($resHistory){
    //   foreach ($resHistory as $key => &$v) {
    //     $v->user_idcard = '*********' . substr($v->user_idcard, 8, 3);
    //     $v->user_name = mb_substr($v->user_name, 0, 1).'*'.mb_substr($v->user_name, 2, 1);
    //     $v->user_phone = substr($v->user_phone, 0, 3).'****' . substr($v->user_phone, 6, 4);
    //   }
    // }
    $return['history'] = $resHistory;
    if (!empty($return['order'])) {
      foreach ($return['order'] as $key => &$v) {
        $v->pay_identity = '*********' . substr($v->pay_identity, 8, 3);
        $v->pay_name = mb_substr($v->pay_name, 0, 1).'*'.mb_substr($v->pay_name, 2, 1);
        $v->contact_phone = substr($v->contact_phone, 0, 3).'****' . substr($v->contact_phone, 6, 4);
        $v->pay_phone = substr($v->pay_phone, 0, 3).'****' . substr($v->pay_phone, 6, 4);
      }
    }
    if (!empty($return['douyin'])) {
      foreach ($return['douyin'] as $key => &$v) {
        $v->identity = '*********' . substr($v->identity, 8, 3);
        $v->name = mb_substr($v->name, 0, 1).'*'.mb_substr($v->name, 2, 1);
        $v->contact = substr($v->contact, 0, 3).'****' . substr($v->contact, 6, 4);
      }
    }
    return $this->returnMsg(200, '查询成功', $return);
  }
  // 查询老系统的订单的数据  点击查询
  public function getOldOder($params)
  {
    return [];
    // $params = $request->all();
    $datas = get_url_content1('http://www.yuxuantongxin.com:32003/api/v1/getAllOrderAndDouYin', $params);
    $datas = json_decode($datas, true);
    $return['oldOrder'] = !empty($datas['data']['order']['data']) ? $datas['data']['order']['data'] : [];
    $return['oldDouyin'] = !empty($datas['data']['douyin']['data']) ? $datas['data']['douyin']['data'] : [];
    return $return;
  }
  // 查询上游的订单 点击查询
  public function getSupOrder(Request $request)
  {
    // 查询数据
    $params = $request->all();
    if (empty($params['channel_id'])) {
      return $this->returnMsg(100, '请传递渠道参数');
    }
    if (empty($params['orderno'])) {
      return $this->returnMsg(100, '请传递订单参数');
    }
    $data = [
      'sup_orderno' => $params['orderno'],
      'model' => '1'
    ];
    $where = [
      'id' => $params['channel_id'],
    ];
    $channelData = DB::table('way')->where($where)->first();
    if (!$channelData) {
      return $this->returnMsg(100, '未查询到该渠道');
    }
    $channelData = json_decode(json_encode($channelData), true);
    $data = array_merge($data, $channelData);
    $model = new Transfer();
    $res =  $model->queryOneOrder($data);
    return $res;
  }
  // 查询抖店的数据
  public function getDouYinOrder(Request $request)
  {
    $params = $request->all();
    if (empty($params['shop_id'])) {
      return $this->returnMsg(100, '请传递店铺参数');
    }
    if (empty($params['orderno'])) {
      return $this->returnMsg(100, '请传递订单参数');
    }
    $shopData = DB::table('douyin_conf')->where(['id' => $params['shop_id']])->first();
    if (!$shopData) {
      return $this->returnMsg(100, '未查询到该店铺');
    }
    if (empty($shopData->app_shop_id)) {
      return $this->returnMsg(101, '请前去配置店铺');
    }
    $data = [
      'shop_id' => $shopData->app_shop_id,
      'pid' => $params['orderno'],
    ];
    $douyinModel = new DouYin($shopData->app_shop_id, $shopData->app_key, $shopData->app_secret);
    $resDouYinDetails = $douyinModel->orderDetails($data);
    if ($resDouYinDetails['code'] != 200) {
      return $this->returnMsg(200, '查询成功', $resDouYinDetails['data']);
    }
    return $resDouYinDetails;
  }
}
